using System;
using System.Collections.Generic;
using System.Text;

namespace Library.IniFiles
{
    public class IniSection
    {
        public const string HEADER_SECTION_TITLE = "_~_h3@d3r_~_";

        private string title;
        private bool isHeaderSection = false;
        private SortedList<string, List<string>> keyValuesCollection;
        private IniFile iniFile;

        public IniSection(string title)
        {
            this.title = title;
        }

        public IniFile IniFile
        {
            get
            {
                return iniFile;
            }
            set
            {
                iniFile = value;
            }
        }

        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
            }
        }

        public bool IsHeaderSection
        {
            get
            {
                if (title == HEADER_SECTION_TITLE)
                {
                    this.isHeaderSection = true;
                }
                return isHeaderSection;
            }
            set
            {
                if (isHeaderSection)
                {
                    this.title = HEADER_SECTION_TITLE;
                }
                isHeaderSection = value;
            }
        }

        private bool IndexerOverwrites
        {
            get
            {
                bool overwrite = false;
                if (iniFile != null)
                {
                    overwrite = this.iniFile.IndexerOverwrites;
                }
                return overwrite;
            }
        }

        public string this[string key]
        {
            get
            {
                if (!KeyValuesCollection.ContainsKey(key))
                {
                    return string.Empty;
                }
                else
                {
                    string val = "";
                    if (KeyValuesCollection[key].Count > 0)
                    {
                        val = KeyValuesCollection[key][0];
                    }
                    return val;
                }
            }
            set
            {
                if ((value != string.Empty) && (key != string.Empty))
                {
                    List<string> values;
                    bool containsKey = KeyValuesCollection.ContainsKey(key);
                    if (containsKey && !IndexerOverwrites)
                    {
                        values = KeyValuesCollection[key];
                        if (!values.Contains(value))
                        {
                            values.Add(value);
                        }
                    }
                    else
                    {
                        if (containsKey) values = KeyValuesCollection[key];
                        else 
                        {
                            values = new List<string>();
                            KeyValuesCollection[key] = values;
                        }

                        if (values.Count == 0) values.Add(value);
                        else if (values.Count == 1) values[0] = value;
                        else if (!values.Contains(value)) values.Add(value);

                    }
                }
            }
        }

        public SortedList<string, List<string>> KeyValuesCollection
        {
            get
            {
                if (keyValuesCollection == null)
                {
                    keyValuesCollection = new SortedList<string, List<string>>();
                }
                return keyValuesCollection;
            }
        }
    }
}
